package com.study;

import java.util.HashMap;

public class Leetcode {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        }


        ListNode result = null, tmp = null;

        boolean l1end = false, l2end = false;
        int plus = 0;
        while (!l1end || !l2end) {
            int sum = 0;

            if (!l1end) {
                sum += l1.val;
            }
            if (!l2end) {
                sum += l2.val;
            }
            if (l1.next == null) {
                l1end = true;
            } else {
                l1 = l1.next;
            }
            if (l2.next == null) {
                l2end = true;
            } else {
                l2 = l2.next;
            }

            if (tmp == null) {
                tmp = new ListNode((sum % 10 + plus) % 10);

            } else {
                tmp.next = new ListNode((sum % 10 + plus) % 10);
                tmp = tmp.next;
            }
            if (sum / 10 > 0 || (sum % 10 + plus) / 10 > 0) {
                plus = 1;
            } else {
                plus = 0;
            }
            if (result == null) {
                result = tmp;
            }

        }
        if (plus != 0) {
            tmp.next = new ListNode(plus);
            tmp = tmp.next;
        }
        return result;
    }

    class AllOne {

        HashMap<String, Integer> map = new HashMap<String, Integer>();

        String max = null;
        String min = null;

        /**
         * Initialize your data structure here.
         */
        public AllOne() {

        }

        /**
         * Inserts a new key <Key> with value 1. Or increments an existing key by 1.
         */
        public void inc(String key) {
            if(key != null && key != "") {

                if (!map.containsKey(key)) {
                    map.put(key, 1);
                } else {
                    map.put(key, map.get(key) + 1);
                }
                if (max == null || map.get(max) < map.get(key)) {
                    max = key;
                }
            }
        }

        /**
         * Decrements an existing key by 1. If Key's value is 1, remove it from the data structure.
         */
        public void dec(String key) {
            if(key != null && key != "") {
                if (map.get(key) != null) {
                    if (map.get(key) == 1) {
                        //todo  給min 賦值

                        map.remove(key);
                    } else {
                        map.put(key, map.get(key) - 1);
                    }
                }
                if (min == null || map.containsKey(key) && map.get(min) > map.get(key)) {
                    max = key;
                }
            }
        }

        /**
         * Returns one of the keys with maximal value.
         */
        public String getMaxKey() {
            if (max == null) {
                return "";
            }else {
                return max;
            }
        }

        /**
         * Returns one of the keys with Minimal value.
         */
        public String getMinKey() {
            if (min == null) {
                return "";
            }else {
                return min;
            }
        }
    }

/**
 * Your AllOne object will be instantiated and called as such:
 * AllOne obj = new AllOne();
 * obj.inc(key);
 * obj.dec(key);
 * String param_3 = obj.getMaxKey();
 * String param_4 = obj.getMinKey();
 */
}

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}